;=============================================================================
; File.......................:	inidb.asm
; Project....................:	IniDB
; Version....................:	1.1
; Update.....................:	13.09.10
;								
; Author.....................:	DeGhost
;								
; Description................:	small simple database(based on ini-files) functions 
; 								
; Comment....................:
;
;		WritePrivateProfileInt lpApplicationName, lpKeyName, dwValue, lpFileName
;
;		IniDB_GetStringEx  lpIniDBHeader, lpszKeyName, lpszDefault, lpszReturnedString
;		IniDB_GetString lpIniDBHeader, lpszKeyName, lpszReturnedString
;		IniDB_SetString lpIniDBHeader, lpszKeyName, lpszString
;		IniDB_GetIntEx lpIniDBHeader, lpszKeyName, dwDefault
;		IniDB_GetInt lpIniDBHeader, lpszKeyName
;		IniDB_SetInt lpIniDBHeader, lpszKeyName, dwInt
;		
;		IniDB_GetItemsNum lpIniDBHeader
;		IniDB_GetItemString lpIniDBHeader, dwIndex, lpszReturnedString
;		IniDB_SetItemString lpIniDBHeader, dwIndex, lpszString
;		IniDB_GetItemInt lpIniDBHeader, dwIndex
;		IniDB_GetItemsNum lpIniDBHeader
;		IniDB_MoveItem lpIniDBHeader, dwDestIndex, dwSourceIndex
;		IniDB_DeleteItem lpIniDBHeader, dwIndex
;		IniDB_AddItemString lpIniDBHeader, lpszString
;								
;								
;=============================================================================


	include 'inidb.inc'
	
	

	szIntegerFmt	db '%i', 0		; for wsprintf function



; The WritePrivateProfileInt function saves the integer value in the ini-file
;
; Parameters
;
;	lpApplicationName	ptr section name string (i.e. 'general')
;	lpKeyName		ptr to key name string (i.e. 'WindowWidth')
;	dwValue			int value to save
;	lpFileName		ptr file path and name string (i.e. 'settings.ini')
;
; Return Value
;	
;	eax != 0		Success
;	eax = 0			Fail
;
; Comment
;
proc WritePrivateProfileInt lpApplicationName, lpKeyName, dwValue, lpFileName
  
  locals
	temp_buffer db INIDB_INTEGER_BUFFER_SIZE dup ?
  endl
	
	lea	eax, [temp_buffer]
	cinvoke	wsprintf, eax, szIntegerFmt, [dwValue]
	lea	eax, [temp_buffer]
	invoke	WritePrivateProfileString, [lpApplicationName], [lpKeyName], eax, [lpFileName]

	ret
endp



; The IniDB_GetStringEx function gets the string value from the ini-file
;
; Parameters
;
;	lpIniDBHeader		ptr INIDB_HEADER structure
;	lpszKeyName		ptr key name string (i.e. 'WindowTitle')
;	lpszDefault		ptr default returned string
;	lpszReturnedString	ptr buffer for returned string
;
; Return Value
;	
;	eax != 0		Success, the return value is the number of characters copied to the buffer, not including the terminating null character.
;
; Comment
;
proc IniDB_GetStringEx  lpIniDBHeader, lpszKeyName, lpszDefault, lpszReturnedString

	mov	eax, [lpIniDBHeader]
	invoke	GetPrivateProfileString, [eax + INIDB_HEADER.lpszIniApplicationName], [lpszKeyName], [lpszDefault], [lpszReturnedString], [eax + INIDB_HEADER.dwStringSize], [eax + INIDB_HEADER.lpszIniFilePath]

	ret
	
endp



; The IniDB_GetString function gets the string value from the ini-file
;
; Parameters
;
;	lpIniDBHeader		ptr INIDB_HEADER structure
;	lpszKeyName		ptr key name string (i.e. 'WindowTitle')
;	lpszReturnedString	ptr buffer for returned string
;
; Return Value
;	
;	eax != 0		Success, the return value is the number of characters copied to the buffer, not including the terminating null character.
;
; Comment
;
proc IniDB_GetString lpIniDBHeader, lpszKeyName, lpszReturnedString
	
	mov	eax, [lpIniDBHeader]
	invoke	GetPrivateProfileString, [eax + INIDB_HEADER.lpszIniApplicationName], [lpszKeyName], [eax + INIDB_HEADER.lpszDefault], [lpszReturnedString], [eax + INIDB_HEADER.dwStringSize], [eax + INIDB_HEADER.lpszIniFilePath]

	ret
	
endp



; The IniDB_SetString function sets the string value in the ini-file
;
; Parameters
;
;	lpIniDBHeader		ptr INIDB_HEADER structure
;	lpszKeyName		ptr key name string (i.e. 'WindowTitle')
;	lpszString		ptr buffer for returned string
;
; Return Value
;	
;	eax != 0		Success
;	eax  = 0		Fail
;
; Comment
;
proc IniDB_SetString lpIniDBHeader, lpszKeyName, lpszString
	
	mov	eax, [lpIniDBHeader]
	invoke	WritePrivateProfileString, [eax + INIDB_HEADER.lpszIniApplicationName], [lpszKeyName], [lpszString], [eax + INIDB_HEADER.lpszIniFilePath]

	ret
	
endp



; The IniDB_GetIntEx function gets the integer value from the ini-file
;
; Parameters
;
;	lpIniDBHeader		ptr INIDB_HEADER structure
;	lpszKeyName		ptr key name string (i.e. 'WindowWidth')
;	dwDefault		int default value 
;
; Return Value
;	
;	eax  = int value	Success
;	eax  = int dwDefault	Fail		
;
; Comment
;
proc IniDB_GetIntEx lpIniDBHeader, lpszKeyName, dwDefault

	mov	eax, [lpIniDBHeader]
	invoke	GetPrivateProfileInt, [eax + INIDB_HEADER.lpszIniApplicationName], [lpszKeyName], [dwDefault], [eax + INIDB_HEADER.lpszIniFilePath]

	ret
	
endp



; The IniDB_GetInt function gets the integer value from the ini-file
;
; Parameters
;
;	lpIniDBHeader		ptr INIDB_HEADER structure
;	lpszKeyName		ptr key name string (i.e. 'WindowWidth')
;
; Return Value
;	
;	eax  = int value	Success
;	eax  = int dwDefault	Fail		
;
; Comment
;
proc IniDB_GetInt lpIniDBHeader, lpszKeyName

	mov	eax, [lpIniDBHeader]
	invoke	GetPrivateProfileInt, [eax + INIDB_HEADER.lpszIniApplicationName], [lpszKeyName], [eax + INIDB_HEADER.dwDefault], [eax + INIDB_HEADER.lpszIniFilePath]

	ret
	
endp



; The IniDB_SetInt function sets the integer value in the ini-file
;
; Parameters
;
;	lpIniDBHeader		ptr INIDB_HEADER structure
;	lpKeyName		ptr to key name string (i.e. 'WindowWidth')
;	dwValue			int value to save
;
; Return Value
;	
;	eax != 0		Success
;	eax = 0			Fail
;
; Comment
;
proc IniDB_SetInt lpIniDBHeader, lpszKeyName, dwValue
	
	mov	eax, [lpIniDBHeader]
	stdcall	WritePrivateProfileInt, [eax + INIDB_HEADER.lpszIniApplicationName], [lpszKeyName], [dwValue], [eax + INIDB_HEADER.lpszIniFilePath]

	ret
	
endp



; The IniDB_GetItemString function gets the string value of the custom item from the ini-file
;
; Parameters
;
;	lpIniDBHeader		ptr INIDB_HEADER structure
;	dwEntryIndex		int index of item
;	lpszReturnedString	ptr buffer for returned string
;
; Return Value
;	
;	eax != 0		Success, the return value is the number of characters copied to the buffer, not including the terminating null character.
;	eax  = 0		Fail		
;
; Comment
;
proc IniDB_GetItemString lpIniDBHeader, dwIndex, lpszReturnedString

  locals
	lpTmpBuffer	dd ?
  endl
	
	push	esi
	
	xor	eax, eax
	push	eax

	invoke	GlobalAlloc, GPTR, INIDB_STRING_SIZE
	test	eax, eax
	jz	.done
	mov	[lpTmpBuffer], eax
	
	mov	esi, [lpIniDBHeader]
	cinvoke	wsprintf, [lpTmpBuffer], [esi + INIDB_HEADER.lpszIniEntryNameFmt], [dwIndex]
	
	invoke	GetPrivateProfileString, [esi + INIDB_HEADER.lpszIniApplicationName], [lpTmpBuffer], [esi + INIDB_HEADER.lpszDefault], [lpszReturnedString], [esi + INIDB_HEADER.dwStringSize], [esi + INIDB_HEADER.lpszIniFilePath]
	mov	[esp], eax

;  .global_free:
	invoke	GlobalFree, [lpTmpBuffer]

  .done:	
	pop	eax
	
	pop	esi
	ret
endp


; The IniDB_SetItemString function sets the string value of the custom item in the ini-file
;
; Parameters
;
;	lpIniDBHeader		ptr INIDB_HEADER structure
;	dwIndex			int index of item
;	lpsString		ptr buffer, string to be written
;
; Return Value
;	
;	eax != 0		Success
;	eax  = 0		Fail		
;
; Comment
;
proc IniDB_SetItemString lpIniDBHeader, dwIndex, lpszString
  
  locals
	lpTmpBuffer	dd ?
  endl
	push	esi

	xor	eax, eax
	push	eax

	invoke	GlobalAlloc, GPTR, INIDB_STRING_SIZE
	test	eax, eax
	jz	.done
	mov	[lpTmpBuffer], eax

	mov	esi, [lpIniDBHeader]
	cinvoke	wsprintf, [lpTmpBuffer], [esi + INIDB_HEADER.lpszIniEntryNameFmt], [dwIndex]
	
	invoke	WritePrivateProfileString, [esi + INIDB_HEADER.lpszIniApplicationName], [lpTmpBuffer], [lpszString], [esi + INIDB_HEADER.lpszIniFilePath]
	mov	[esp], eax

;  .global_free:
	invoke	GlobalFree, [lpTmpBuffer]

  .done:	
	pop	eax
	
	pop	esi
	ret
endp



; The IniDB_GetItemInt function gets the integer value of the custom item from the ini-file
;
; Parameters
;
;	lpIniDBHeader		ptr INIDB_HEADER structure
;	dwIndex			int index of item
;
; Return Value
;	
;	eax  = dwValue		Success
;	eax  = dwDefault	Fail		
;
; Comment
;
proc IniDB_GetItemInt lpIniDBHeader, dwIndex
  
  locals
  	lpTmpBuffer	dd ?
  endl
  	
  	push	esi
  
	xor	eax, eax
	push	eax

	invoke	GlobalAlloc, GPTR, INIDB_STRING_SIZE
	test	eax, eax
	jz	.done
	mov	[lpTmpBuffer], eax

	mov	esi, [lpIniDBHeader]
	cinvoke	wsprintf, [lpTmpBuffer], [esi + INIDB_HEADER.lpszIniEntryNameFmt], [dwIndex]
	
	invoke	GetPrivateProfileInt, [esi + INIDB_HEADER.lpszIniApplicationName], [lpTmpBuffer], [esi + INIDB_HEADER.dwDefault], [esi + INIDB_HEADER.lpszIniFilePath]
	mov	[esp], eax

;  .global_free:
	invoke	GlobalFree, [lpTmpBuffer]

  .done:	
	pop	eax
	
	pop	esi
	ret
endp



; The IniDB_GetItemsNum function gets the number of the items from the ini-file
;
; Parameters
;
;	lpIniDBHeader		ptr INIDB_HEADER structure
;
; Return Value
;	
;	eax  = dwNumberOfItems	Success
;	eax  = -1		Fail		
;
; Comment
;
proc IniDB_GetItemsNum lpIniDBHeader

  locals
	lpTmpBuffer	dd ?
	lpszDefault	dd ?
	lpszReturnedString dd ?
  endl
  	
  	push	esi
  
	xor	eax, eax
	dec	eax
	push	eax

	invoke	GlobalAlloc, GPTR, INIDB_STRING_SIZE + 1 + INIDB_STRING_SIZE 
	test	eax, eax
	jz	.done
	mov	[lpTmpBuffer], eax
	add	eax, INIDB_STRING_SIZE
	mov	[lpszDefault], eax
	inc	eax
	mov	[lpszReturnedString], eax
	
	inc	dword [esp]
 @@:
	mov	esi, [lpIniDBHeader]
	cinvoke	wsprintf, [lpTmpBuffer], [esi + INIDB_HEADER.lpszIniEntryNameFmt], dword [esp]
	
	invoke	GetPrivateProfileString, [esi + INIDB_HEADER.lpszIniApplicationName], [lpTmpBuffer], [esi + INIDB_HEADER.lpszDefault], [lpszReturnedString], INIDB_STRING_SIZE, [esi + INIDB_HEADER.lpszIniFilePath]
	test	eax, eax
	jz	.global_free
	
	inc	dword [esp]
	jmp	@b	

  .global_free:
	invoke	GlobalFree, [lpTmpBuffer]

  .done:
	pop	eax
	
	pop	esi
	ret

endp



; The IniDB_MoveItem function moves the item on the custom position in the ini-file
;
; Parameters
;
;	lpIniDBHeader		ptr INIDB_HEADER structure
;	dwDestIndex		int destination item index
;	dwSourceIndex		int source item index
;
; Return Value
;	
;	eax  = 1		Success
;	eax  = 0		Fail		
;
; Comment
;
proc IniDB_MoveItem lpIniDBHeader, dwDestIndex, dwSourceIndex
  
  locals
	lpszSource	dd ?
	lpszDest	dd ?
	lpszDefault	dd ?
	lpTmpBuffer	dd ?
  endl
  
	xor	eax, eax
	push	eax

	invoke	GlobalAlloc, GPTR, 1 + INIDB_STRING_SIZE + 2*(INIDB_STRING_SIZE)
	test	eax, eax
	jz	.done
	mov	[lpszDefault], eax
	inc	eax
	mov	[lpTmpBuffer], eax
	add	eax, INIDB_STRING_SIZE
	mov	[lpszSource], eax
	add	eax, INIDB_STRING_SIZE
	mov	[lpszDest], eax
	
	stdcall	IniDB_GetItemString, [lpIniDBHeader], [dwSourceIndex], [lpszSource]
	test	eax, eax
	jz	.global_free
	
	stdcall	IniDB_GetItemString, [lpIniDBHeader], [dwDestIndex], [lpszDest]
	test	eax, eax
	jz	.global_free
	
	stdcall	IniDB_SetItemString, [lpIniDBHeader], [dwSourceIndex], [lpszDest]
	
	stdcall	IniDB_SetItemString, [lpIniDBHeader], [dwDestIndex], [lpszSource]
	
	inc	dword [esp] 
	
  .global_free:
	invoke	GlobalFree, [lpszDefault]
	
  .done:
	pop	eax
	
	ret
endp



; The IniDB_DeleteItem function removes the item on the custom position from the ini-file
;
; Parameters
;
;	lpIniDBHeader		ptr INIDB_HEADER structure
;	dwIndex			int index of item to be deleted
;
; Return Value
;	
;	eax  = 1		Success
;	eax  = 0		Fail		
;
; Comment
;
proc IniDB_DeleteItem lpIniDBHeader, dwIndex
	
	push	esi
	push	ebx
	
	xor	eax, eax
	push	eax


	mov	ebx, [dwIndex]
	mov	esi, ebx
	inc	esi

  @@:

	stdcall	IniDB_MoveItem, [lpIniDBHeader], ebx, esi
	test	eax, eax
	jz	@f

	inc	ebx
	inc	esi
	jmp	@b

  @@:
	stdcall	IniDB_SetItemString, [lpIniDBHeader], ebx, 0
	inc	dword [esp]
	

	pop	eax
	
	pop	ebx
	pop	esi	
	ret
endp



; The IniDB_AddItemString function adds the string item on the end position in the ini-file
;
; Parameters
;
;	lpIniDBHeader		ptr INIDB_HEADER structure
;	lpszString		ptr to the string will be written
;
; Return Value
;	
;	eax != 0		Success
;	eax  = 0		Fail		
;
; Comment
;
proc IniDB_AddItemString lpIniDBHeader, lpszString
		
	stdcall	IniDB_GetItemsNum, [lpIniDBHeader]
	stdcall	IniDB_SetItemString, [lpIniDBHeader], eax, [lpszString]

	ret
endp





